#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 110 + 10;
const int mod = 31011;

int mat[N][N], tot, n, m, etot, id[N];
struct {
	int fa[N];
	void reset() { rep(i, 1, n) fa[i] = i; }
	int gf(int x) { return fa[x] == x ? x : fa[x] = gf(fa[x]); }
	bool merge(int x, int y) { x = gf(x), y = gf(y); if(x == y) return false; fa[x] = y; return true; }
} t1, t2;
tuple < int, int, int > c[N << 4], ed[N << 4];

int calc(const veg &p1, const veg &p2, const veg &lst) {
	t2.reset(); for(auto v : lst) t2.merge(v.fi, v.se);
	tot = 0; rep(i, 1, n) if(t2.gf(i) == i) id[i] = ++tot; else id[i] = 0;
	rep(i, 1, n) id[i] = id[t2.gf(i)];
	for(auto v : p2) {
		int x = v.fi, y = v.se; x = id[x], y = id[y];
		mat[x][x]++; mat[y][y]++; mat[x][y]--; mat[y][x]--;
	}
	rep(i, 1, tot) rep(j, 1, tot) mat[i][j] = (mat[i][j] % mod + mod) % mod;//  cerr << mat[i][j] << endl;
	int ans = 1;
	rep(i, 1, tot - 1) {
		int t = i;
		rep(j, i, tot - 1) if(mat[j][i]) { t = j; break; }
		if(t ^ i) swap(mat[t], mat[i]), ans = mod - ans;
		rep(j, i + 1, tot - 1)
			while(mat[j][i]) {
				int d = mat[j][i] / mat[i][i];
				rep(k, i, tot) mat[j][k] -= 1ll * d * mat[i][k] % mod, mat[j][k] += mod, mat[j][k] %= mod;
				if(mat[j][i] == 0) break;
				swap(mat[j], mat[i]); ans = mod - ans;
			}
	}
	//rep(i, 1, tot) { rep(j, 1, tot) cerr << mat[i][j] << " "; cerr << endl; }
	//	cerr << ans << endl;
	rep(i, 1, tot - 1) ans = 1ll * ans * mat[i][i] % mod;//err << ans << endl;
	rep(i, 1, tot) rep(j, 1, tot) mat[i][j] = 0;
	return ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in, m = in; rep(i, 1, m) { int u = in, v = in, w = in; c[i] = make_tuple(w, u, v); }
	sort(c + 1, c + m + 1);
	t1.reset();
	rep(i, 1, m) {
		int x, y, w; tie(w, x, y) = c[i];
		if(t1.merge(x, y)) ed[++etot] = c[i];
	}
	if(etot != n - 1) return puts("0"), 0;
	int cur = 1, ans = 1;
	rep(l, 1, etot) {
		vector < pii > v1, v2, lst;	int r = l;
		while(r <= etot && get<0>(ed[r]) == get<0>(ed[l])) v1.eb(get<1>(ed[r]), get<2>(ed[r])), r++;
		while(cur <= m && get<0>(c[cur]) != get<0>(ed[l])) cur++;
		while(cur <= m && get<0>(c[cur]) == get<0>(ed[l])) v2.eb(get<1>(c[cur]), get<2>(c[cur])), cur++;
		rep(i, 1, l - 1) lst.eb(get<1>(ed[i]), get<2>(ed[i]));
		rep(i, r, etot) lst.eb(get<1>(ed[i]), get<2>(ed[i]));
		ans = 1ll * ans * calc(v1, v2, lst) % mod; l = r - 1;
	} printf("%d\n", ans);
	return 0;
}
